package com.gitee.zinface.java.examples.jna;

import java.util.Timer;

import com.gitee.zinface.java.examples.jna.Hello.CLibrary.User.UserValue;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;

/**
 * Hello
 * 
 * 这是一个基于 Jna 操作的 Hello 示例程序，通过加载 resources 资源目录的扩展库，达到调用C程序库的目的
 */
public class Hello {

    public interface CLibrary extends Library {
        CLibrary INSTANCE = Native.load("jna-hello", CLibrary.class);
        
        // 1
        void hello();
        // 2
        int max(int a, int b);
        // 3
        int array(short[] vals, int len);

        // 4
        @Structure.FieldOrder({"name", "age", "height"})
        public static class User extends Structure {
            public static class UserValue extends User implements Structure.ByValue {
                public UserValue(String name, int age, double height) {
                    super(name, age, height);
                }
            }

            public User(String name, int age, double height) {
                this.name = name;
                this.age = age;
                this.height = height;
            }

            public String name;
            public int age;
            public double height;
        }

        void printUser(User user);
    }

    public static void main(String[] args) {
        
        // 1 
        // 调用 jna-lib/main.c:3 处的 hello 函数， 打印 Hello World
        CLibrary.INSTANCE.hello();

        // 2
        // 调用 jna-lib/main.c:11 处的 max 函数，打印最大值
        System.out.println(CLibrary.INSTANCE.max(1, 2));

        // 3
        // 调用 jna-lib/main.c:16 处的 array 函数，打印数组信息
        CLibrary.INSTANCE.array(new short[]{1,2,3,4}, 4);

        // 4
        // 基本的结构体使用
        UserValue userValue = new CLibrary.User.UserValue("zinface", 18, 1.75);
        CLibrary.INSTANCE.printUser(userValue);
        
    }
}